package com.cjt.chat.service.impl;

import com.cjt.chat.mapper.MessageMapper;
import com.cjt.chat.mapper.UserMapper;
import com.cjt.chat.pojo.Message;
import com.cjt.chat.pojo.User;
import com.cjt.chat.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
 * @author cjt
 * @since 2024/3/15
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private MessageMapper messageMapper;

    @Override
    public User queryOneUser(User user) {
        return userMapper.queryOneUser(user);
    }

    @Override
    public List<User> queryFriendList(User user) {
        List<Integer> friendIdList = userMapper.queryFriendList(user);
        return userMapper.queryUsersByUseridList(friendIdList);
    }

    @Override
    public void setUserHeadImage(String url, Integer userId) {
        userMapper.setUserHeadUrl(url, userId);
    }

    @Override
    public User searchUser(String username, Integer userId) {
        User user = userMapper.queryUserByUsername(username);
        Boolean isFriend = userMapper.queryIsFriend(userId, user.getUserid()) != null;
        user.setIsFriend(isFriend);
        return user;
    }

    @Override
    public void addFriend(Integer friendId, Integer userId, Integer messageId) {
        userMapper.addFriend(friendId, userId);
        messageMapper.deleteMessageByMessageId(messageId);
    }

    @Override
    public void modifyUserPetname(Integer userId, String petname) {
        userMapper.modifyUserPetname(userId, petname);
    }

    @Override
    public void deleteFriend(Integer friendId, Integer userId) {
        userMapper.deleteFriend(friendId, userId);
        messageMapper.deleteMessageByUserIdFriendId(friendId, userId);
    }

    @Override
    public List<User> getUserDetailByUserIdList(List<Integer> userIds) {
        return userMapper.queryUsersByUseridList(userIds);
    }

    @Override
    public Boolean registerUser(String username, String petName, String password) {
        String usrename = userMapper.usernameExist(username);
        if (usrename == null) {
            userMapper.addUser(username, petName, password);
            return true;
        }
        return false;
    }
}
